package com.jams.goodox.activity.serve.praise;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lvman.configs.instrumentation.InstrumentedDraweeView;
import com.jams.goodox.R;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.entity.ComplaintPriseBean;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.Utils;
import com.jams.library.commonAdapter.ListCommonAdapter;
import com.jams.library.commonAdapter.ListCommonViewHolder;
import com.jams.library.fresco.FrescoUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 投诉表扬列表页
 * Created by jams_zhen on 2016/9/19.
 */
public class ComplaintPraiseListActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    @Bind(R.id.lst)
    ListView lst;

    private ListCommonAdapter lstAdapter;
    private List<ComplaintPriseBean> complaintPriseBeanBeanList;

    private Intent in;
    private String hintMsg="找不到具体人员时，可直接%s整个部门";
    private String title;

    @Override
    protected int getLayoutId() {
        return R.layout.good_bad_list_activity;
    }

    @Override
    protected void init() {
        in = getIntent();
        title = in.getStringExtra("title")+"";
        complaintPriseBeanBeanList = (List<ComplaintPriseBean>) in.getSerializableExtra("list");

        StyleUtil.titleBackKey(this, title);
        if (complaintPriseBeanBeanList == null) {
            complaintPriseBeanBeanList = new ArrayList<>();
        }

        lstAdapter = new ListCommonAdapter<ComplaintPriseBean>(this, complaintPriseBeanBeanList, R.layout.item_praise_item) {
            @Override
            public void convert(ListCommonViewHolder helper, ComplaintPriseBean item, int position) {
                TextView tx_hint = helper.getView(R.id.tx_hint);
                InstrumentedDraweeView img_head = helper.getView(R.id.img_head);
                TextView tx_name = helper.getView(R.id.tx_name);
                TextView tx_department = helper.getView(R.id.tx_department);
                TextView tx_num = helper.getView(R.id.tx_num);
                ImageView img_choose = helper.getView(R.id.img_choose);
                View line_bottom = helper.getView(R.id.line_bottom);

                if (position == 0) {
                    tx_hint.setVisibility(View.VISIBLE);
                    tx_hint.setText(String.format(hintMsg,title.contains("表扬")?"表扬":"投诉"));
                    line_bottom.setVisibility(View.GONE);
                } else {
                    tx_hint.setVisibility(View.GONE);
                    if (position + 1 != complaintPriseBeanBeanList.size()) {
                        line_bottom.setVisibility(View.VISIBLE);
                    } else {
                        line_bottom.setVisibility(View.GONE);
                    }
                }

                if (!TextUtils.isEmpty(item.getHeadPicName())) {
                    FrescoUtil.loadImg(img_head, item.getHeadPicName());
                } else {
                    FrescoUtil.loadImg(img_head, "");
                }
                tx_name.setText(item.getUserName() + "");
                tx_department.setText("" + item.getAdminDuty());
                tx_num.setText(item.getPraiseCount() + "");
                if (item.isChoose()) {
                    img_choose.setImageResource(R.mipmap.check_theme);
                } else {
                    img_choose.setImageResource(R.mipmap.uncheck_icon);
                }
            }
        };
        lst.setAdapter(lstAdapter);
        lst.setOnItemClickListener(this);
    }

    @OnClick(R.id.tx_sure)
    public void onClick() {
        if (Utils.isFastDoubleClick())
            return;
        in = new Intent();
        in.putExtra("list", (Serializable) complaintPriseBeanBeanList);
        setResult(RESULT_OK, in);
        finish();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (Utils.isFastDoubleClick())
            return;
        int len = complaintPriseBeanBeanList.size();
        //选中项为部门时
        if (position == 0) {
            if (complaintPriseBeanBeanList.get(0).isChoose()) {
                complaintPriseBeanBeanList.get(0).setChoose(false);
            } else {
                if (len > 1) {
                    for (int i = 1; i < len; i++) {
                        complaintPriseBeanBeanList.get(i).setChoose(false);
                    }
                }
                complaintPriseBeanBeanList.get(0).setChoose(true);
            }
        } else {
            //点击普通成员，仅当点击未选择项时才会选中
            if (!complaintPriseBeanBeanList.get(position).isChoose()) {
                complaintPriseBeanBeanList.get(position).setChoose(true);
                complaintPriseBeanBeanList.get(0).setChoose(false);
            } else {
                complaintPriseBeanBeanList.get(position).setChoose(false);
            }
        }
        lstAdapter.notifyDataSetChanged();
    }
}
